local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- 配置
local TARGET_NAME = "pssmurf0077"
local OFFER_WAIT = 2
local ACCEPT_DELAY = 10

-- 获取远程事件
local function GetRemote(name)
    local remote = ReplicatedStorage.Trade:FindFirstChild(name)
    assert(remote, "找不到远程事件: "..name)
    return remote
end

local SendRequest = GetRemote("SendRequest")
local UpdateTrade = GetRemote("UpdateTrade")
local AcceptTrade = GetRemote("AcceptTrade")

local TradeState = {
    OfferId = nil,
    ItemsReceived = false,
    FirstAcceptSent = false,
    OtherAccepted = false
}

local function Log(action, msg)
    local time = os.date("%H:%M:%S")
    print(string.format("[%s][%s] %s", time, action, msg))
end

local function CallRemote(remote, arg1, arg2)
    local success, result = pcall(function()
        if remote:IsA("RemoteEvent") then
            if arg2 ~= nil then
                remote:FireServer(arg1, arg2)
            else
                remote:FireServer(arg1)
            end
        else
            if arg2 ~= nil then
                return remote:InvokeServer(arg1, arg2)
            else
                return remote:InvokeServer(arg1)
            end
        end
        return true
    end)

    if not success then
        Log("ERROR", string.format("%s调用失败: %s", remote.Name, tostring(result)))
    else
        Log("SUCCESS", string.format("%s调用: %s %s", 
            remote.Name, 
            tostring(arg1), 
            arg2 ~= nil and tostring(arg2) or ""))
    end
    return success
end
-- 检测到物品后响应
local function OnItemsDetected()
    if not TradeState.OfferId or TradeState.FirstAcceptSent then return end

    Log("ACTION", "检测到物品，2秒后发送首次AcceptTrade")
    task.delay(OFFER_WAIT, function()
        CallRemote(AcceptTrade, TradeState.OfferId, false)
        TradeState.FirstAcceptSent = true

        -- 15秒后发送最终确认，使用固定ID
        task.delay(ACCEPT_DELAY, function()
            if not TradeState.OtherAccepted then
                Log("WARNING", "对方仍未接受，尝试强制确认 (使用固定ID)")
                CallRemote(AcceptTrade, 285646582, true)  -- ★ 固定ID ★
            end
        end)
    end)
end

local function OnOtherAccepted()
    if TradeState.OtherAccepted then return end

    TradeState.OtherAccepted = true
    Log("ACTION", "对方已接受，立即确认 (使用固定ID)")
    CallRemote(AcceptTrade, 285646582, true)  -- ★ 固定ID ★
end

UpdateTrade.OnClientEvent:Connect(function(data)
    if not data then return end

    if data.LastOffer and data.LastOffer ~= TradeState.OfferId then
        TradeState.OfferId = data.LastOffer
        Log("INFO", "新交易ID: "..data.LastOffer)
    end

    if data.Player2 and data.Player2.Offer and #data.Player2.Offer > 0 then
        if not TradeState.ItemsReceived then
            TradeState.ItemsReceived = true
            Log("INFO", string.format("收到%d件物品: %s", 
                #data.Player2.Offer, 
                data.Player2.Offer[1][1]))
            OnItemsDetected()
        end
    end

    if data.Player2 and data.Player2.Accepted then
        OnOtherAccepted()
    end
end)
local function StartTrade()
    local target = Players:FindFirstChild(TARGET_NAME)
    if not target then
        Log("ERROR", "目标玩家不在线")
        return false
    end

    Log("INFO", "开始与 "..TARGET_NAME.." 交易")
    return CallRemote(SendRequest, target)
end

task.spawn(function()
    if StartTrade() then
        Log("INFO", "交易流程已启动")
    else
        Log("ERROR", "无法启动交易")
    end
end)